package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;


    @Autowired
    private VodService vodService;


    @TsLogin
    @Operation(summary = "获取用户下单的对应声音列表(过滤已经购买过的)")
    @GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable Long trackId,
                                              @PathVariable Integer trackCount) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId() ;
        //调用服务层方法
        List<TrackInfo> trackInfoList = trackInfoService.findPaidTrackInfoList(trackId, trackCount, userId);
        return Result.ok(trackInfoList);
    }



    /**
     * 获取用户声音购买列表
     * @param trackId
     * @return
     */
    @TsLogin
    @Operation(summary = "获取声音分集购买列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result  findUserTrackPaidList(@PathVariable("trackId") Long trackId) {
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法：
        List<Map<String,Object>> map = trackInfoService.findUserTrackPaidList(trackId, userId);
        //  返回数据
        return Result.ok(map);
    }


    /**
     * 获取专辑对应的声音列表
     * @param albumId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @TsLogin
    @Operation(summary = "获取专辑对应的声音列表")
    @GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
    public Result findAlbumTrackPage(@PathVariable Long albumId,
                                     @PathVariable Integer pageNo,
                                     @PathVariable Integer pageSize) {
    //获取用户id
        Long userId = null == AuthContextHolder.getUserId() ? 1l : AuthContextHolder.getUserId();
        //构建分页对象
        Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pageNo, pageSize);
        //调用服务层方法
        IPage<AlbumTrackListVo> iPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage, albumId, userId);
        return Result.ok(iPage);
    }

    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        //调用服务器方法,返回一个map集合,返回媒体应用id及媒体url
        Map<String, Object> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    @TsLogin
    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        //获取用户ID
        Long userId = null == AuthContextHolder.getUserId() ? 1l : AuthContextHolder.getUserId();
        //调用服务器方法
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();
    }


    @TsLogin
    @Operation(summary = "获取声音分页列表")
    @PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
    public Result findUserTrackPage(@PathVariable("pageNo") Long pageNo,
                                    @PathVariable("pageSize") Long pageSize,
                                    @RequestBody TrackInfoQuery trackInfoQuery) {
        Page<TrackListVo> page = new Page<>(pageNo, pageSize);
        Long userId = null == AuthContextHolder.getUserId() ? 1l : AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        //调用服务器方法
        IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(page, trackInfoQuery);
        //返回数据
        return Result.ok(trackListVoIPage);
    }


    @Operation(summary = "删除声音信息")
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId) {
        //调用服务器方法
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }


    @Operation(summary = "删除时要根据id回显声音信息")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable Long trackId) {
        //调用服务器方法
        TrackInfo trackInfo= trackInfoService.getById(trackId);
        return Result.ok(trackInfo);
    }


	@Operation(summary = "修改声音信息")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable Long trackId,
								  @RequestBody TrackInfoVo trackInfoVo) {
		//调用服务器方法
		trackInfoService.updateTrackInfo(trackId, trackInfoVo);
		return Result.ok();
	}
}



